package com.smalls.redshoes.util;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AsyncImageLoader {
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + "AsyncImageLoader":"AsyncImageLoader";
    private HashMap<String, Object> imageCache = new HashMap();
    private List<Thread> threadList = new ArrayList();

    public interface ImageCallback {
        void imageLoaded(Drawable drawable, String str);
    }

    public void loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
        if (this.imageCache.containsKey(imageUrl)) {
            Drawable drawable = (Drawable) this.imageCache.get(imageUrl);
            if (drawable != null) {
                imageCallback.imageLoaded(drawable, imageUrl);
                return;
            }
        }
        final Handler handler = new Handler() {
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
            }
        };
        Thread thread = new Thread() {
            public void run() {
                Drawable drawable = AsyncImageLoader.this.loadImageFromUrl(imageUrl);
                AsyncImageLoader.this.imageCache.put(imageUrl, drawable);
                handler.obtainMessage(0, drawable).sendToTarget();
            }
        };
        thread.start();
        this.threadList.add(thread);
    }

    public Drawable loadImageFromUrl(String url) {
        Drawable drawable = null;
        if (!(url == null || url.trim().equals(""))) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost post = new HttpPost(url);
                post.setEntity(new UrlEncodedFormEntity(new ArrayList(), HTTP.UTF_8));
                HttpResponse response = httpClient.execute(post);
                if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    if (is != null) {
                        drawable = Drawable.createFromStream(is, "src");
                    } else {
                        //SuperLog.d(this.TAG, "loadImageFromUrl-is = null");
                    }
                    is.close();
                }
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return drawable;
    }

    public void close() {
        if (this.threadList != null) {
            for (Thread thread : this.threadList) {
                if (thread != null) {
                    thread.interrupt();
                }
            }
        }
    }
}
